package model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import util.LogTable;
import util.Name;
import util.Pair;


public class Log {
	
	private ArrayList<String> operatorList = new ArrayList<String>();
	private QueryRec rec = new QueryRec();
	
//	private Map<String,Integer> opCount = new HashMap<String,Integer>();
//	private Map<String,ArrayList<Pair>> opIndex = new HashMap<String,ArrayList<Pair>>();
	//private ArrayList<String> sessionOps = new ArrayList<String>(); 
	
	//private Map<String,Map<String,Integer>> pairMap = null;
	
	
	public Log(){
	}
	

	/**Load Index from database
	 * */
	public void loadIndex(){
		
	}
	
	/*Store the update to database
	 * */
//	private void updateIndex(Connection conn) throws Exception{
//		
//		for(String op:operatorList){
//			int count = opCount.get(op);
//			ArrayList<Pair> opList = opIndex.get(op);
//			
//			boolean first = true;
//			String opStr = "";
//			for(Pair p : opList){
//				if(!first){
//					opStr += p;
//					first = false;
//				}
//				else{
//					opStr += (","+p);
//				}
//				
//			}
//			
//			String sql = "Insert into cube.operatorindex (operator,count,adjacentcount) values(?,?,?)  on duplicate key update count=?,adjacentcount=?";
//			PreparedStatement stmt = conn.prepareStatement(sql);
//			
//			stmt.setString(1, op);
//			stmt.setInt(2, count);
//			stmt.setString(3, opStr);
//			stmt.setInt(4, count);
//			stmt.setString(5, opStr);
//			
//			stmt.executeUpdate();
//			
//		}
//	}
	
	/**Update pair
	 * */
	private void updatePairMap(Map<String,Map<String,Integer>> pairMap){
		
		String pre ="";
		Map <String,Integer> innerMap = null;
		
		for(String op:operatorList){
			if(pre.length()==0){
				pre = op;
				continue;
			}
			else{
				if(pairMap.containsKey(pre)){
					innerMap = pairMap.get(pre);
					if(innerMap.containsKey(op)){
						innerMap.put(op, innerMap.get(op)+1);
					}
					else{
						innerMap.put(op, 1);
					}
				}
				else{
					innerMap = new HashMap<String,Integer>();
					innerMap.put(op, 1);
				}
				pairMap.put(pre, innerMap);
			}
			
			pre = op;
		}

	}
//	private void updateOpCount(String operator){	
//		if(opCount.containsKey(operator)){
//			opCount.put(operator, opCount.get(operator)+1);
//		}
//		else{
//			opCount.put(operator, 1);
//		}	
//	}
//	
//	
//	
//	private void updateOpIndex(String pre,String cur){
//		if(opIndex.containsKey(pre)){
//			ArrayList <Pair> list = opIndex.get(pre);
//			
//			boolean flag = false;
//			
//			for(int i=0;i<list.size();i++){
//				
//				String key = list.get(i).key;
//				Integer count = list.get(i).val;
//				
//				if(key.equals(cur)){
//					flag = true;
//					list.set(i, new Pair(key,count+1));
//					break;
//				}
//			}
//			if(!flag){
//				list.add(new Pair(pre,1));
//			}
//			
//			opIndex.put(pre, list);
//			
//		}
//		else{
//			Pair pl = new Pair(cur,1);
//			ArrayList <Pair> list = new ArrayList<Pair>();
//			list.add(pl);
//			opIndex.put(pre, list);
//		}
//	}
//	
	
	
	public int store(String type,Connection conn,Map<String,Map<String,Integer>> pairMap,LogTable logtable) throws Exception{
		
		String sRootPath = new File("").getAbsolutePath();
		System.out.println(sRootPath);

//		FileWriter fr = new FileWriter("log.dat",true);
//		BufferedWriter bw = new BufferedWriter(fr);
		
		String res = "";
		boolean flag = true;
		
		System.out.println("operationList:"+operatorList);
		
		if(operatorList.size()==1&&operatorList.get(0).equals("all:none")){
			//bw.close();
			return 0;
		}
		
		
		
		String pre = "";
		
//		for(String op:operatorList){
//			//updateOpCount(op);
//			if(flag){
//				res += op;
//				flag =  false;
//			}
//			else{
//				res += (","+op);
//				//updateOpIndex(pre,op);
//			}
//			pre = op;
//		}
		
		//String sql = "Insert into logtable (session) values (?)";
		//PreparedStatement stmt = conn.prepareCall(sql);
		
		//System.out.println("ressss:"+res+" "+res.equals("all:none"));
		
		if(operatorList.size()>=2&&!operatorList.get(0).equals("all:none")){
			//stmt.setString(1, res);
			//bw.write(res.toCharArray());
			//stmt.executeUpdate();
			updatePairMap(pairMap);
			System.out.println("logMap:"+pairMap);
			//updateIndex(conn);
		}
	//	bw.close();
		int ret = operatorList.size() - 1;
		operatorList.clear();
		logtable.selMap.clear();
		logtable.selMap.put(Name.level0, "none");
		logtable.selMap.put(Name.level1,"none");
		logtable.selMap.put(Name.level2,"none");
		
		logtable.selMap.put(Name.country,"none");
		logtable.selMap.put(Name.city,"none");
		
		
		logtable.selMap.put(Name.year,"none");
		logtable.selMap.put(Name.month,"none");
		logtable.selMap.put(Name.day,"none");
		return ret;
	}
	
	private boolean isValidOperator(String op){
		
		return false;
	}
	
	public JsonObject add(String operator,LogTable logtable){
		operatorList.add(operator);
		String [] ops = operator.split(":");
		if(ops[0].equals("drill") ||ops[0].equals("all")){
			logtable.selMap.put(ops[0], logtable.dict.get(ops[1]));
			//logtable.selMap.put(logtable.dict.get(ops[1]), ops[2]);
		}
		else if( ops[0].equals("roll")){
			logtable.selMap.put(logtable.dict.get(ops[1]),"none");
		}
		else if(ops[0].equals("slice")){
			logtable.selMap.put(logtable.dict.get(ops[1]), ops[2]);
		}
		
		
		if(logtable.opCountMap.containsKey(operator)){
			logtable.opCountMap.put(operator,logtable.opCountMap.get(operator)+1);
		}
		else{
			logtable.opCountMap.put(operator, 1);
		}
		logtable.opCount++;
		
		JsonObject obj = new JsonObject();
		
		if(operator.equals("all:none"))
		{
			obj = rec.initRec(4, logtable);
		}
		else{
			obj = rec.recQueries(4, operator, logtable);
		}
		
		return obj;
	}
}
